----------------------------------------------------------------------------------------------------------------------------------
local NPC_BASE_LIB = Class(function(self, inst)
    self.inst = inst

    self.DataTable = {}
    self.tempData = {}

    self.inst.npc_base_lib = self
end,
nil,
{

})
------------------------------------------------------------------------------------------------------------------------------
require("components/npc_libs/npc_lib_spawn_func")():Init(NPC_BASE_LIB) --- 刷新NPC的事件func入口

require("components/npc_libs/00_npc_lib_base_fn")():Init(NPC_BASE_LIB) --- 基础的常用 func
require("components/npc_libs/01_npc_lib_base_movement_fn")():Init(NPC_BASE_LIB) --- 基础移动 func
require("components/npc_libs/02_npc_lib_base_actions")():Init(NPC_BASE_LIB) --- 基础动作 func
require("components/npc_libs/03_npc_lib_gift_list")():Init(NPC_BASE_LIB) --- 物资列表

require("components/npc_libs/04_npc_lib_spawn_fx")():Init(NPC_BASE_LIB) --- 特效相关操作

require("components/npc_libs/05_npc_lib_base_special_charactors_skin_fn")():Init(NPC_BASE_LIB) --- DLC角色皮肤相关的操作

require("components/npc_libs/06_npc_lib_base_for_sale_func")():Init(NPC_BASE_LIB) --- 可交易建筑物相关

require("components/npc_libs/07_npc_lib_rpc")():Init(NPC_BASE_LIB) --- 辅助RPC系统

------------------------------------------------------------------------------------------------------------------------------

function NPC_BASE_LIB:Close_2_Point_In_Range(_table)
    -- _table = {
    --     point = Vector3(0,0,0),
    --     range = 10,
    --     talk = {
    --          str = "66666",
    --          once_flag = false,
    --          once_flag_num = 0,
    --      },
    --      fn = function  ----    fn(theNPC,_table)
    --      fn_moving = function  fn_moving(theNPC,_table)
    --      stop_flag = false 
    --      walk = nil,
    -- }

    if _table and _table.point then
        if _table.range == nil then
            _table.range = 0.5
        end
        local Distance = self:Dsitance_T2P(self.inst,_table.point)
        if Distance < _table.range then
            if _table.stop_flag == false then
                ---
            else
                self:Stop()
            end
            -- self:Stop()


            if _table.fn and type(_table.fn) == "function" then
                _table.fn(self.inst,_table)
            end
            return true
        else
            self:GoToPoint(_table.point,_table.walk)
            if _table.fn_moving and type(_table.fn_moving) == "function" then
                _table.fn_moving(self.inst,_table)
            end
            return false
        end
    end
end


function NPC_BASE_LIB:Close_2_Target_In_Range(_table)
    -- _table = {
    --     target = inst,
    --     range = 10,
    --     talk = {
    --          str = "66666",
    --          once_flag = false,
    --          once_flag_num = 0,
    --      }，
    --      fn = function  ----    fn(theNPC,_table)
    --      stop_flag = false 
    --      fn_moving = function fn(theNPC,_table)
    --      walk = nil
    -- }
    if _table == nil then
        return false
    end

    if _table.target == nil then
        _table.target = self.inst.__link_player
    end

    if _table.target == nil or _table.range == nil then
        print("Error : NPC_BASE_LIB:Close_2_Target_In_Range , no target . no range")
        return false
    end

    local _target_point = Vector3(_table.target.Transform:GetWorldPosition())
    local Distance = self:Dsitance_T2P(self.inst,_target_point)
    if Distance <= _table.range then
        if _table.stop_flag == false then
            ---
        else
            self:Stop()
        end
        -- self:Stop()


        if _table.fn and type(_table.fn) == "function" then
            _table.fn(self.inst,_table)
        end
        return true
    else
        self:GoToPoint(_target_point,_table.walk)
        if _table.fn_moving and type(_table.fn_moving) == "function" then
            _table.fn_moving(self.inst,_table)
        end
        return false
    end


end

function NPC_BASE_LIB:Run_Around_Target_In_Range(_table)
    -- _table = {
    --     target = inst or Point, -- 
    --     range = 10,
    --     random_range_flag = false,
    --     mid_reflash_range = 2    ----- 目标移动后，判断移动距离并重新定义基点
    --     talk = {
    --         str = "...",
    --     }, 
    --      fn = funcion  ----    fn(theNPC,_table)
    --     clean ==  false  ----- 如果为 true 则清空临时数据
    --     walk = nil
    -- }

    --------------------------------------------------------------------------
    ---- --- tempData 重定向
    if _table and _table.clean == true then
        self.tempData.Run_Around_Target_In_Range = nil
        return
    end
    
    if self.tempData.Run_Around_Target_In_Range == nil then
        self.tempData.Run_Around_Target_In_Range = {}
    end
    local tempData = self.tempData.Run_Around_Target_In_Range
    --------------------------------------------------------------------------

    if _table and _table.range then
        local theplayer = self.inst.__link_player

        local BasePoint = Vector3(0,0,0)
        local StopRange = _table.range

        if _table.target == nil and theplayer then
            BasePoint = Vector3(theplayer.Transform:GetWorldPosition())
        elseif _table.target and _table.target.x then --- target is Vector3()
            BasePoint = _table.target
        elseif _table.target and _table.target.x == nil and _table.target.Transform then --- target is inst
            BasePoint = Vector3(_table.target.Transform:GetWorldPosition())
        end

        if _table.random_range_flag == true and  StopRange > 3 then
            StopRange = math.random(3,StopRange)
        end





        if tempData.around_2_point == nil then
            tempData.around_2_point = self:GetSpawnPoint(BasePoint,StopRange)
        else


            --- -- 目标移动后刷新围绕基点
            if tempData.around_base_point_last then
                local last_current_distance = self:Distance_Points(BasePoint,tempData.around_base_point_last)
                local compare_range = _table.mid_reflash_range or 2
                if last_current_distance > compare_range then
                    tempData.around_2_point = self:GetSpawnPoint(BasePoint,StopRange)
                end
            end
            tempData.around_base_point_last = BasePoint



            local ret = self:Close_2_Point_In_Range({
                point = tempData.around_2_point,
                range = 0.5,
                talk = _table.talk,
                fn = _table.fn,
                walk = _table.walk
            })

            if ret == true then ---
                tempData.around_2_point = nil
                --- say
                -- if _table.talk then
                --     self.inst:DoTaskInTime(0.2,function()
                --         self:Say(_table.talk)                
                --     end)
                -- end
                
                return true
            else
                return false
            end


        end



        return false
    end
end

function NPC_BASE_LIB:Run_Surround_Target_In_Range(_table)
    -- _table = {
    --     target = inst or Point, -- 
    --     range = 10,
    --     num = 8             ----- 坐标数量
    --     mid_reflash_range = 2    ----- 目标移动后，判断移动距离并重新定义基点
    --     reverse_flag = nil            ---反转列表跑
    --      fn = funcion  ----    fn(theNPC,_table)
    --      clean  = false          -- true 的时候清空数据
    -- }
    
    ------------------------------------------------------------------------
    -- -- 临时 tempData 重定向
    if _table and _table.clean == true then
        self.tempData.Run_Surround_Target_In_Range = {}
    end

    if self.tempData.Run_Surround_Target_In_Range == nil then
        self.tempData.Run_Surround_Target_In_Range = {}
    end
    local tempData = self.tempData.Run_Surround_Target_In_Range


    ------------------------------------------------------------------------


    if _table == nil then return end

    local reverse_flag = _table.reverse_flag

    local Mid_pt = nil
    if _table.target == nil then
        return
    end

    if _table.target.x then
        Mid_pt = _table.target
    elseif _table.target.prefab then
        Mid_pt = Vector3( _table.target.Transform:GetWorldPosition() )
    end

    if Mid_pt == nil then return end

    local num = _table.num or 8


    --- -- 获取环绕坐标点
    if tempData.Surround_Points == nil then
        tempData.Surround_Points = self:GetSurroundPoints({target = Mid_pt,range = 8,num = num})
    end

    ----------- target 移动后重新获取环绕坐标点
    if tempData.Surround_Mid_Point_Last then
        local mid_reflash_range = _table.mid_reflash_range or 2
        local tempDistacne = self:Distance_Points(Mid_pt,tempData.Surround_Mid_Point_Last)
        if tempDistacne > mid_reflash_range then
            tempData.Surround_Points = self:GetSurroundPoints({target = Mid_pt,range = 8,num = num})
        end
    end
    tempData.Surround_Mid_Point_Last = Mid_pt

    ----------- 安全检查
    if tempData.Surround_Points == nil then
        return
    elseif type(tempData.Surround_Points) ~= "table" then
        return
    elseif #tempData.Surround_Points <  3 then
        return            
    end

    -- if tempData.reverse_flag_last ~= reverse_flag then
    --     tempData.surround_next_point_num = nil
    -- end
    -- tempData.reverse_flag_last = reverse_flag



    -------------
    local surround_points = tempData.Surround_Points

    if tempData.surround_next_point_num == nil then
                ---- 先找到最近的点过去
        tempData.surround_next_point_num = 1
        tempData.surround_next_point = surround_points[1]
        local temp_min_distance = self:Dsitance_T2P(self.inst,tempData.surround_next_point)
        

        for k, temp_point in pairs(surround_points) do
            local temp_dis = self:Dsitance_T2P(self.inst,temp_point)
            if  temp_dis <= temp_min_distance then
                temp_min_distance = temp_dis
                tempData.surround_next_point = temp_point
                tempData.surround_next_point_num = k
            end
        end
    end


    ---------------



    local function surround_next_point_change(theNPC)
        local tempData = theNPC.npc_base_lib.tempData.Run_Surround_Target_In_Range
        if reverse_flag ~= true then    ------ 正向
            tempData.surround_next_point_num = tempData.surround_next_point_num + 1
            if tempData.surround_next_point_num > #surround_points then
                tempData.surround_next_point_num = 1
            end
        else        ---------------------------反向
            tempData.surround_next_point_num = tempData.surround_next_point_num - 1
            if tempData.surround_next_point_num < 1 then
                tempData.surround_next_point_num = #surround_points
            end
        end
        tempData.surround_next_point = surround_points[tempData.surround_next_point_num]
    end 
    self:Close_2_Point_In_Range({
        point = tempData.surround_next_point,
        range = 2,
        fn = surround_next_point_change,
        stop_flag = false
    })

    -- self.tempData.Run_Surround_Target_In_Range = tempData

end

return NPC_BASE_LIB